/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package iammry.com.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.view.View;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import java.util.TimeZone;

import iammry.com.R;
import iammry.com.mode.ChartType;
import iammry.com.utils.CalculateDayofMonth;

/**
 * Average temperature demo chart.
 */
public class AverageCubicTemperatureChart extends AbstractDemoChart {

    String[] weekDays = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期日"};
    /**
     * Returns the chart name.
     *
     * @return the chart name
     */
    public String getName() {
        return "Average temperature";
    }

    /**
     * Returns the chart description.
     *
     * @return the chart description
     */
    public String getDesc() {
        return "The average temperature in 4 Greek islands (cubic line chart)";
    }

    GraphicalView view;
    private Random random=new Random();

    double[] xcache = new double[20];
    /**数据*/
    double[] ycache = new double[20];
    /**
     * Executes the chart demo.
     *
     * @param context the context
     * @return the built intent
     */
    @SuppressWarnings("deprecation")
    public View getView(Context context, ChartType type, double[] value1, double[] value2) {

        String[] titles = new String[]{"晚上", "早上"};
        List<double[]> x = new ArrayList<double[]>();
        for (int i = 0; i < titles.length; i++) {
            x.add(new double[]{0, 1, 2, 3, 4, 5, 6});
        }
        List<double[]> values = new ArrayList<double[]>();


        if (value1.length == 0 || value2.length == 0) {
            value1 = new double[]{40, 30, 50, 25, 80, 60, 45};
            value2 = new double[]{20, 60, 30, 25, 70, 90, 10};

        }

        values.add(value1);
        values.add(value2);

        int xColor = context.getResources().getColor(R.color.LINE_ONE);
        int yColor = context.getResources().getColor(R.color.LINE_TWO);

        int[] colors = new int[]{xColor, yColor};

        //没有用到
        PointStyle[] styles = new PointStyle[]{PointStyle.CIRCLE, PointStyle.DIAMOND
        };

        XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);

        int length = renderer.getSeriesRendererCount();

        for (int i = 0; i < length; i++) {
            ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
        }

        setChartSettings(renderer, 0, 100, 0, 7,
                R.color.button_selector, R.color.button_selector);

        if (type == ChartType.MONTH) {
            setChartSettings(renderer, 0, 100, 0, 30,
                    R.color.button_selector, R.color.button_selector);

        }
        renderer.setRange(new double[]{0, 7, 0, 100});//xy
        renderer.setYLabels(5);
        renderer.setXLabels(0);
        renderer.setShowGridX(true);
        renderer.setGridColor(R.color.button_selector);
        renderer.setXLabelsAlign(Align.CENTER);
        renderer.setYLabelsAlign(Align.RIGHT);

        renderer.setZoomButtonsVisible(false);
        //设置禁止放大
        renderer.setZoomEnabled(false, false);
        //  renderer.setXLabels(0);

        if (type == ChartType.MONTH) {

            renderer.setPanLimits(new double[]{0, 31, 0, 0});

        } else if (type == ChartType.WEEK)

        {
            renderer.setPanLimits(new double[]{0, 0, 0, 0});

        }
        renderer.setZoomLimits(new double[]{0, 20, 0, 40});
        renderer.setBackgroundColor(Color.WHITE);//�
        renderer.setMarginsColor(Color.WHITE);

        renderer.setInScroll(true);
        if (type == ChartType.WEEK) {

            for (int i = 0; i < 7; i++) {
                renderer.addTextLabel(i,weekDays[i]);
            }

        } else if (type == ChartType.MONTH) {

            List<String> datas = getMonthDate();
            for (int i = 0; i < datas.size(); i++) {
                renderer.addTextLabel(i, datas.get(i));
            }

        }


        view = ChartFactory.getCubeLineChartView(context, buildDataset(titles, x, values),
                renderer, 0.33f);

        return view;
    }


    public void updateChart() {
        //设定长度为20
        int length = series.getItemCount();
        if(length>=20) length = 20;
        int addY=random.nextInt()*100;
        int addX=random.nextInt();
        //将前面的点放入缓存
        for (int i = 0; i < length; i++) {
            xcache[i] = series.getX(i);
            ycache[i] = (int) series.getY(i);
        }
        series.clear();
        series.add(addX, addY);
        for (int k = 0; k < length; k++) {
            series.add(xcache[k], ycache[k]);
        }
        //在数据集中添加新的点集
        dataset.removeSeries(series);
        dataset.addSeries(series);
        //曲线更新
        view.invalidate();
    }

    /**
     *
     */
    public static String getWeekDate() {
        List<String> dates = new ArrayList<String>();
        final Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);// ���
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int week = c.get(Calendar.WEEK_OF_YEAR);
        String data = getFirstDayOfWeek(mYear, week);
        return data;

    }

    public static String getFirstDayOfWeek(int year, int week) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置周
        cal.set(Calendar.WEEK_OF_YEAR, week);
        //设置该周第一天为星期一
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfWeek = sdf.format(cal.getTime());

        return firstDayOfWeek;
    }

    /**
     * �õ����굱�µ��������
     **/
    public static int MaxDayFromDay_OF_MONTH(int year, int month) {
        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, year);
        time.set(Calendar.MONTH, month - 1);//��
        int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);//���·ݵ�����
        return day;
    }


    public static List<String> getMonthDate() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int mYear = c.get(Calendar.YEAR);// ���
        int mMonth = c.get(Calendar.MONTH) + 1;// ��
        List<String> dates = new ArrayList<String>();

        int day = CalculateDayofMonth.getDay(mYear, mMonth);
        for (int i = 1; i <= day; i++) {
            String item = mMonth + "." + i;
            dates.add(item);

        }
        return dates;
    }


}
